package com.smart.manager.member.entities.bodys;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.crypto.digest.DigestUtil;
import com.smart.common.entities.core.BaseSearch;
import com.smart.common.entities.domain.SmartPattern;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.io.Serializable;


/**
 * @author xiaxuqiu
 * @date 2023/03/26
 */
public class RMemberFriend {


    @Data
    @EqualsAndHashCode(callSuper = true)
    @ApiModel("RMemberFriend1")
    public static class SearchBody extends BaseSearch {


        @ApiModelProperty("等级代码")
        private String code;


        @ApiModelProperty("等级名称")
        private String name;


    }


    @Data
    @ApiModel("RMemberFriend2")
    public static class ApplyBody implements Serializable {
        @ApiModelProperty("关系接收人ID")
        @NotNull(message = "关系接收人ID 不能为空")
        private Long receiverId;
    }


    @Data
    @ApiModel("RMemberFriend3")
    public static class AuditBody implements Serializable {
        @ApiModelProperty("好友关系ID")
        @NotNull(message = "好友关系ID 不能为空")
        private String token;

        @ApiModelProperty("审批意见 true =同意申请 false=拒绝申请")
        @NotNull(message = "审批意见 不能为空")
        private Boolean approval;
    }




    @Data
    @ApiModel("RMemberFriend4")
    public static class SearchFriendBody implements Serializable {
        @ApiModelProperty("搜索手机号")
        @NotBlank(message = "搜索友手机号")
        @Pattern(regexp = SmartPattern.PHONE,message = "手机号码格式错误")
        private String phoneNumber;

        @ApiModelProperty("时间戳")
        private Long timestamp ;


        @ApiModelProperty("签名字符串  算法:大写32位MD5(SMART-手机号码-时间戳) ,中间有短横线 ")
        @NotBlank(message = "搜索好友签名")
        private String sign ;

        public void checkSign() {
            String sourceStr = String.format("SMART-%s-%s", this.phoneNumber, this.timestamp);
            String sign2 = DigestUtil.md5Hex(sourceStr).toUpperCase();
            Assert.equals(this.sign, sign2, "搜索好友签名失败");
            long abs = Math.abs(DateUtil.currentSeconds() - this.timestamp);
            Assert.isTrue(abs <= 300L, "设备时间异常:请校验当前设备的时间后再进行好友搜索");
        }
    }
}
